<template>
	<div>
		<!-- 面包屑导航 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
		  <el-breadcrumb-item :to="{ path: '/welcom' }">首页</el-breadcrumb-item>
		  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
		  <el-breadcrumb-item>商品列表</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 卡片视图 -->
		<el-card class="box-card">
			<el-row :gutter="20">
				<el-col :span="9" class="grid-content bg-purple">
					<el-input  v-model="queryInfo.query" placeholder="请输入内容" clearable @clear="getGoodsList">
					     <el-button @click="getGoodsList" slot="append" icon="el-icon-search"></el-button>
					  </el-input>
				</el-col>
				<el-col :span="8" class="grid-content bg-purple">
					<el-button type="primary" class="button-circle" @click="toAddPage">添加商品</el-button>
				</el-col>
			</el-row>
			<!-- 商品表格 -->
			 <el-table
			    :data="goodslist"
			    stripe
			    style="width: 100%"
				border>
				<el-table-column type="index" label="#"></el-table-column>
			    <el-table-column
			      prop="goods_name"
			      label="商品名称"
			      >
			    </el-table-column>
			    <el-table-column
			      prop="goods_price"
			      label="商品价格(元)"
			      width="120px">
			    </el-table-column>
				<el-table-column
				  prop="goods_weight"
				  label="商品重量(g)"
				  width="100px">
				</el-table-column>
			    <el-table-column
			      label="创建时间"
				  width="170px">
				  <template slot-scope="scope">
					  {{scope.row.add_time | dateFormat}}
				  </template>
			    </el-table-column>
				<el-table-column width="120px" label="操作">
					<template slot-scope="scope">
						<el-button @click="editDialog(scope.row)" type="primary" size="mini" icon="el-icon-edit" circle></el-button>
						<el-button @click="deleteGoods(scope.row.goods_id)" type="danger" size="mini" icon="el-icon-delete" circle></el-button>
					</template>
				</el-table-column>
			  </el-table>
			  <!-- 分页 -->
			 <el-pagination
			      @size-change="handleSizeChange"
			      @current-change="handleCurrentChange"
			      :current-page="queryInfo.pagenum"
			      :page-sizes="[10, 15, 20, 30]"
			      :page-size="queryInfo.pagesize"
			      layout="total, sizes, prev, pager, next, jumper"
			      :total="total"
				  background>
			    </el-pagination>
		</el-card>
		 <el-dialog
       title="修改商品参数"
       :visible.sync="editDialogVisible"
       width="30%"
       :before-close="handleClose">
      <el-form ref="addFormRef" :model="addForm" :rules="addFormRules">
		  <el-form-item prop="goods_name" label="商品名称">
		<el-input class="dialogInput" v-model="addForm.goods_name" type="text"></el-input>
		  </el-form-item>
		    <el-form-item prop="goods_price" label="商品价格">
		<el-input class="dialogInput" v-model="addForm.goods_price" type="number"></el-input>
		  </el-form-item>
		    <el-form-item prop="goods_weight" label="商品重量">
		<el-input class="dialogInput" v-model="addForm.goods_weight" type="number"></el-input>
		  </el-form-item>
	  </el-form>
       <span slot="footer" class="dialog-footer">
         <el-button @click="editDialogVisible = false">取 消</el-button>
         <el-button type="primary" @click="editGoodsSubmit">确 定</el-button>
       </span>
     </el-dialog>
	</div>
</template>

<script>
	export default{
		data(){
			return{
				searchInfo:'',
				editGoods:{},
				queryInfo:{
					query:'',
					pagenum:1,
					pagesize:10
				},
				total:0,
				goodslist:[],
				editDialogVisible:false,
				// 编辑修改商品数据的表单
				addForm:{
					goods_name:'',
					goods_price:'',
					goods_weight:''
				},
				 addFormRules: {
                   goods_name: [
                     { required: true, message: '请输入商品名称', trigger: 'blur' },
                     { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
					],
					goods_price: [
                     { required: true, message: '请输入商品价格', trigger: 'blur' },
					],
					goods_weight: [
                     { required: true, message: '请输入商品重量', trigger: 'blur' },
					]
				 }
			}
		},
		methods:{
			//获取商品列表数据
			async getGoodsList(){
				const { data:res } = await this.$http.get('goods',  {
					params: this.queryInfo }
				)
				//console.log(res)
				if ( res.meta.status !== 200 )
				return this.$message.error(res.meta.msg)
				this.goodslist = res.data.goods
				this.total = res.data.total
				//console.log(this.goodslist)
			},
			// 改变分页
			handleSizeChange(newSize){
				this.queryInfo.pagesize = newSize
				this.getGoodsList()
				
			},
			handleCurrentChange(newNum){
				this.queryInfo.pagenum = newNum
				this.getGoodsList()
			},
			toAddPage(){
				this.$router.push('/goods/add')
			},
			//删除商品
			deleteGoods(goodId){
				  this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
			          confirmButtonText: '确定',
			          cancelButtonText: '取消',
			          type: 'warning'
			        }).then(async() => {
					 const {data:res} = await this.$http.delete('goods/'+goodId)
					 if( res.meta.status !== 200 )
					 return res.meta.msg 
			          this.$message({
			            type: 'success',
			            message: '删除成功!'
			          });
					this.getGoodsList()
			        }).catch(() => {
			          this.$message({
			            type: 'info',
			            message: '已取消删除'
			          });          
			        });
			},
			async editDialog(row){
			
			//console.log(this.addForm)
			const { data:res } = await this.$http.get('goods/'+ row.goods_id )
			if( res.meta.status !== 200 )
			return this.$message.error('获取商品信息失败')
			
			this.addForm = res.data
			//console.log(this.addForm)
            this.editDialogVisible = true
			},
			editGoodsSubmit(){
              	this.$refs.addFormRef.validate( async valid => {
					if(!valid){
						return this.$message.error('数据格式有误！')
					}
					
					// 发起添加请求
					const { data:res } = await this.$http.put('goods/'+ this.addForm.goods_id, this.addForm)

					if(res.meta.status !== 200)
					{
						//console.log(res)
						return this.$message.error(res.meta.msg)
					}
					this.$message.success('修改商品成功！')
					this.editDialogVisible = false
					this.getGoodsList()
				})
			},
			handleClose(){
				this.$refs.addFormRef.resetFields()
				this.editDialogVisible = false
			}
		},
		created() {
			this.getGoodsList()
		}
	}
</script>

<style lang="less" scoped>
	.el-col{
		margin-left: 10px;
	}
	.el-table .el-button{
		margin: 8px;
	}
</style>
